﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Drawing;
using System.Collections;

public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txt_name.Focus();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/SysYcjy/createuser_free.aspx", true);
    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        MembershipUser zmem = Membership.GetUser(txt_name.Text.Trim());
        if (zmem == null)
        {
            ClientScript.RegisterStartupScript(GetType(), "loginError", string.Format("alert('{0}');", "您的登录尝试不成功,请重试"), true);
            ClientScript.RegisterStartupScript(GetType(), "focusSelect", "document.getElementById('txt_name').select();", true);
            return;
        }
        if (Membership.ValidateUser(txt_name.Text.Trim(), txt_pwd.Text.Trim()))
        {
            string username = txt_name.Text.Trim();

            Dictionary<string, string> userList = Application.Get("GLOBAL_USER_LIST") as Dictionary<string, string>;
            if (userList != null)
            {
                string userKey = string.Empty;
                foreach (var item in userList)
                {
                    if (item.Value != null && String.Compare(item.Value, username) == 0)
                    {
                        userKey = item.Key;
                        userList[userKey] = "useronline";
                        break;
                    }
                }
            }
            else
            {
                userList = new Dictionary<string, string>();
            }
            userList[Session.SessionID] = username;
            Application.Lock();
            Application.Add("GLOBAL_USER_LIST", userList);
            Application.UnLock();
            //FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);
            //来自:http://www.2ed.cn/Article-111-15374.shtml
            FormsAuthentication.SetAuthCookie(txt_name.Text.Trim(), true);//仅用此功能无法实现用户登录！string x = Membership.GetUser().UserName.TrimEnd();时x为null
            //FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);
            //string zxdpurl0 = "main_temp.aspx";
            //Response.Redirect(zxdpurl0);
            //Server.Transfer("main_temp.aspx");    //此语句造成验证结果无法传递
            // Response.Redirect("main_temp.aspx");
            Response.Redirect("iframe.aspx", true);
        }
        else
        {
            //以下处理验证未通过的提示
            if (zmem.IsLockedOut)
            {
                Panel messageForLocked = new Panel();
                messageForLocked.GroupingText = "系 统 提 示";
                messageForLocked.ID = "messageLock";
                Label lblMessage = new Label();
                lblMessage.Text = "您所使用的账号<i>" + zmem.UserName + "</i><br />由于<b> " + Membership.MaxInvalidPasswordAttempts.ToString()
                + " </b>次密码错误已经被系统锁定<br />请联系平台管理员进行解锁<br /><br />";
                lblMessage.Font.Name = "微软雅黑";
                lblMessage.ForeColor = Color.Red;
                HtmlInputButton btnClose = new HtmlInputButton("button");
                btnClose.Value = "关  闭";
                btnClose.ID = "btnClose";
                btnClose.Attributes.Add("onclick", "ok();");
                messageForLocked.Controls.Add(lblMessage);
                messageForLocked.Controls.Add(btnClose);
                Page.Form.Controls.AddAt(6, messageForLocked);
            }
            else if (!zmem.IsApproved)
            {
                Panel messageForLocked = new Panel();
                messageForLocked.GroupingText = "系 统 提 示";
                messageForLocked.ID = "messageLock";
                Label lblMessage = new Label();
                lblMessage.Text = "您所使用的账号<i>" + zmem.UserName + "</i>已经被平台管理员锁定<br />请联系平台管理员进行解锁<br /><br />";
                lblMessage.Font.Name = "微软雅黑";
                lblMessage.ForeColor = Color.Red;
                HtmlInputButton btnClose = new HtmlInputButton("button");
                btnClose.Value = "关  闭";
                btnClose.ID = "btnClose";
                btnClose.Attributes.Add("onclick", "ok();");
                messageForLocked.Controls.Add(lblMessage);
                messageForLocked.Controls.Add(btnClose);
                Page.Form.Controls.AddAt(6, messageForLocked);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "loginError", string.Format("alert('{0}');", "您的登录尝试不成功,请重试"), true);
            }
            if (!ClientScript.IsStartupScriptRegistered("focusSelect"))
            {
                ClientScript.RegisterStartupScript(GetType(), "focusSelect", "document.getElementById('txt_name').select();", true);
            }
        }
    }
}
